import { deptList, removeDept, addDept, updateRule, deptDetail, sortChange } from '../api/platuser';
import * as api from '../api/platuser';
import { message } from 'antd/lib/index';
import checkStatus from '@/utils/status';

export default {
  namespace: 'platuser',

  state: {
    data: {
      list: [],
      pagination: {},
    },
  },

  effects: {
    * fetch({ payload }, { call, put }) {
      const response = yield call(api.List, payload);
      yield checkStatus(response);
      yield put({
        type: 'getList',
        payload: response.msg,
      });
    },
    * couponList({ payload }, { call, put }) {
      const response = yield call(api.couponList, payload);
      yield checkStatus(response);
      yield put({
        type: 'setCouponList',
        payload: response.msg,
      });
    },
    * send({ payload, callback }, { call, put }) {
      const response = yield call(api.send, payload);
      yield checkStatus(response);
      if (callback) callback(response);
    },
    * userList({ payload, callback }, { call, put }) {
      const response = yield call(api.userList, payload);
      yield checkStatus(response);
      yield put({
        type: 'setUserList',
        payload: response.msg,
      });
      if (callback) callback(response);
    },

    * getDesc({ payload, callback }, { call, put }) {
      const response = yield call(api.Detail, payload);
      yield checkStatus(response);

      yield put({
        type: 'setDesc',
        payload: response.msg,
      });
      if (callback) callback();
    },


  },

  reducers: {
    getList(state, action) {
      state.pagination = {
        pageSize: action.payload.pageSize,
        current: action.payload.pageNo,
        total: action.payload.total,
      };
      return {
        ...state,
        data: action.payload,
      };
    },
    setUserList(state, action) {
      return {
        ...state,
        userList: action.payload,
      };
    },
    setDesc(state, action) {
      return {
        ...state,
        detail: action.payload,
      };
    },
    setCouponList(state, action) {
      return {
        ...state,
        couponList: action.payload,
      };
    },
  },
};
